package com.xbongbong.pay.platform.wechatpay.enums;

import lombok.Getter;

import java.util.Objects;

/**
 * WeChat 支付公共错误码枚举
 * @author 魏荣杰
 * @date 2021/1/11 16:29
 * @since
 * @version
 */
@Getter
public enum WeChatPayErrorEnum {
    /**
     *
     */
    APPID_MCHID_NOT_MATCH(400, "APPID_MCHID_NOT_MATCH", "商户号与appid不匹配"),
    BANK_ERROR(500, "BANK_ERROR", "银行系统异常"),
    OUT_TRADE_NO_USED(403, "OUT_TRADE_NO_USED", "商户订单号重复"),
    REQUEST_BLOCKED(403, "REQUEST_BLOCKED", "此状态代表退款申请失败，商户可根据具体的错误提示做相应的处理"),
    BIZ_ERR_NEED_RETRY(500, "BIZ_ERR_NEED_RETRY", "退款业务流程错误"),
    USER_PAYING(202, "USER_PAYING", "用户支付中，需要输入密码"),
    PARAM_ERROR(400, "PARAM_ERROR", "参数错误"),
    ORDER_NOT_EXIST(404, "ORDER_NOT_EXIST", "请求的资源不存在"),
    CONTRACT_NOT_EXIST(403, "CONTRACT_NOT_EXIST", "签约协议不存在"),
    PHONE_NOT_EXIST(400, "PHONE_NOT_EXIST", "手机号不存在"),
    SIGN_ERROR(401, "SIGN_ERROR", "签名验证失败"),
    ACCOUNT_ERROR(403, "ACCOUNT_ERROR", "账号异常"),
    SYSTEM_ERROR(500, "SYSTEM_ERROR", "5开头的状态码都为系统问题，请使用相同参数 稍后重新调用"),
    AUTH_CODE_INVALID(400, "AUTH_CODE_INVALID", "收银员扫描的不是微信支付的条码"),
    FREQUENCY_LIMITED(429, "FREQUENCY_LIMITED", "频率超限"),
    RATELIMIT_EXCEEDED(429, "RATELIMIT_EXCEEDED", "频率限制"),
    NO_AUTH(403, "NO_AUTH", "商户暂无权限使用此功能"),
    RULE_LIMIT(403, "RULE_LIMIT", "业务规则限制"),
    AUTH_CODE_EXPIRE(403, "AUTH_CODE_EXPIRE", "用户的条码已经过期"),
    TRADE_ERROR(403, "TRADE_ERROR", "交易错误"),
    NOT_ENOUGH(403, "NOT_ENOUGH", "余额不足"),
    USER_NOT_EXIST(404, "USER_NOT_EXIST", "用户账户注销"),
    ERROR(500, "ERROR", "业务错误"),
    FREQUENCY_LIMIT_EXCEED(429, "FREQUENCY_LIMIT_EXCEED", "接口限频"),
    CONTRACT_EXISTED(403, "CONTRACT_EXISTED", "协议已存在"),
    USER_ACCOUNT_ABNORMAL(403, "USER_ACCOUNT_ABNORMAL", "用户账户异常"),
    CONTRACT_ERROR(403, "CONTRACT_ERROR", "当前用户签约状态失效"),
    REFUND_NOT_EXISTS(404, "REFUND_NOT_EXISTS", "订单号错误或订单状态不正确"),
    CONTRACT_NOT_CONFIRMED(403, "CONTRACT_NOT_CONFIRMED", "二级商户未开启手动提现权限"),
    NO_STATEMENT_EXIST(400, "NO_STATEMENT_EXIST", "账单文件不存在"),
    STATEMENT_CREATING(400, "STATEMENT_CREATING", "账单生成中"),
    MCH_NOT_EXISTS(404, "MCH_NOT_EXISTS", "商户号不存在"),
    INVALID_REQUEST(400, "INVALID_REQUEST", "请求参数符合参数格式，但不符合业务规则"),
    RESOURCE_NOT_EXISTS(404, "RESOURCE_NOT_EXISTS", "查询的资源不存在"),
    RESOURCE_ALREADY_EXISTS(400, "RESOURCE_ALREADY_EXISTS", "用户已签约该商户，不可重复签约"),
    ALREADY_EXISTS(400, "ALREADY_EXISTS", "资源已存在"),
    USER_NOT_REGISTERED(400, "USER_NOT_REGISTERED", "服务未开通或账号未注册"),
    USER_NOT_EXISTS(404, "USER_NOT_EXISTS", "openid不正确"),
    ORDER_CLOSED(400, "ORDER_CLOSED", "订单已关闭"),
    ORDER_PAID(400, "ORDER_PAID", "订单已支付"),
    ORDER_REVERSED(400, "ORDER_REVERSED", "订单已撤销"),
    ORDERCLOSED(400, "ORDERCLOSED", "订单已关闭"),
    ORDERPAID(400, "ORDERPAID", "订单已支付"),
    ORDERREVERSED(400, "ORDERREVERSED", "订单已撤销"),
    ACCOUNT_NOT_VERIFIED(403, "ACCOUNT_NOT_VERIFIED", "二级商户下行打款未成功"),
    NOT_FOUND(404, "NOT_FOUND", "请求的资源不存在"),

    ;

    private final int code;
    private final String errorCode;
    private final String describe;

    WeChatPayErrorEnum(int code, String errorCode, String describe) {
        this.code = code;
        this.errorCode = errorCode;
        this.describe = describe;
    }

    public static WeChatPayErrorEnum getCode(Integer code) {
        if (Objects.isNull(code)) {
            return null;
        }
        for (WeChatPayErrorEnum value : values()) {
            if (Objects.equals(value.getCode(), code)) {
                return value;
            }
        }
        return null;
    }
    public static WeChatPayErrorEnum getByErrorCode(String errorCode) {
        if (Objects.isNull(errorCode) || errorCode.isEmpty()) {
            return null;
        }
        for (WeChatPayErrorEnum value : values()) {
            if (Objects.equals(value.getErrorCode(), errorCode)) {
                return value;
            }
        }
        return null;
    }
}
